* Table 9
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
append using "Counterfactuals Stata Files/Counterfactual14.dta"
append using "Counterfactuals Stata Files/Counterfactual11.dta"
append using "Counterfactuals Stata Files/Counterfactual7.dta"


replace experiment = 0 if experiment==16 // benchmark
replace experiment = 1 if experiment==14 // UHI 
replace experiment = 2 if experiment== 11 // zero OOP
replace experiment=3 if experiment==7 // all can treat


keep if age<65
gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  //1/1.045=0.956937799
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
replace earnings_PV=. if age!=25
replace earnings_PV=earnings_PV/1000


gen I_good_H=1 if H==3
replace I_good_H=0 if H!=3

replace I_pay=. if type2==1 | I_treat==0

gen COV=.
tostring COV, replace
foreach num of numlist 0 1 2 3  {
inequal7 earnings_PV if experiment==`num'
replace COV=r(cov) if experiment==`num'
}


gen COV_educ=.
tostring COV_educ, replace
foreach num of numlist 0 1 2 3 {
	foreach educ of numlist 1 2 3{
inequal7 earnings_PV if experiment==`num' & education==`educ'
replace COV_educ=r(cov) if experiment==`num' & education==`educ'
}
}

destring COV , replace
destring COV_educ , replace


preserve
collapse (mean) I_treat I_pay TR I_good_H employed_yn earnings_PV COV , by(experiment )
gen education=0
sort education experiment
save "Temp\PV Earnings Variation.dta", replace
restore 

* by education
collapse (mean) I_treat I_pay TR I_good_H employed_yn earnings_PV  COV_educ, by(experiment education) 
rename COV_educ COV
sort education experiment
append using "Temp\PV Earnings Variation.dta"

order experiment education I_treat I_pay I_good_H employed_yn TR earnings_PV  COV
replace I_treat=I_treat*100
replace I_pay=I_pay*100
replace I_good_H=I_good_H*100
replace employed_yn = employed_yn*100
replace  TR=TR*100

drop if education == 2 | education==3
sort education experiment
* open data and copy paste into table 9 -- all rows and columns are in order 

}